package com.df.bpm.utils.businessViewer.stepsTableServices;

import com.df.bpm.entity.BusinessEntity;
import com.df.bpm.utils.businessViewer.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class EntityService {
    Map<Integer,BusinessEntity> entityMap=new HashMap<>();
//    BusinessStepsTable golden;
//    public EntityService(BusinessStepsTable iTable) {
//        golden=iTable;
//    }


    /**
     * 获取担当主体信息
     * @param entityID  主体ID
     * @return  担当主体信息
     */
    public BusinessEntity getEntity(Integer entityID) {
        BusinessEntity result=entityMap.get(entityID);
        if(result==null){//如果本地没存就去接口获取
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessEntity/id?businessEntityIndex="+entityID;
            ResponseEntity<BusinessEntity> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    new ParameterizedTypeReference<BusinessEntity>() {}
            );
            result=response.getBody();
            entityMap.put(entityID,result);
        }
        return result;
    }

    public Integer updateEntity(BusinessEntity businessEntity) {
        if(businessEntity.getName()!=null
                && !businessEntity.getName().isEmpty()
                && businessEntity.getEntityTypeIndex()!=null
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessEntity/";
            HttpEntity<BusinessEntity> request = new HttpEntity<>(businessEntity, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }
}
